package gluck.sintactico;

import gluck.lexico.Token;
import gluck.sintactico.symbolTable.SymbolTable;
import gluck.sintactico.tree.Tree;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;



public class ByaccParserFix extends Parser{
	
	
	@Override
	void val_push(ParserVal val)
	{
	  if (valptr>=YYSTACKSIZE || val==null){
		  System.err.println("Stack overflow");
		    return;  
	  }
		  

	  if(val.obj!=null)
	  {
		  valstk[++valptr]= new ParserVal(val.obj);  
	  }else{
		  valstk[++valptr]= new ParserVal(val.sval);
	  }
	  
	}
	
	@Override
	public void yyerror (String error) {
		
		System.err.println("\n\nError: " + error + " after '" +
				yytoken.m_text + "' line "+ yytoken.m_line+ 
				" column "+ yytoken.m_column);
		
		String msg= Token.getErrorMessage(yytoken.m_index);
		if(msg!=null){
			System.err.println(msg);
		}
		
		System.exit(1);
		
	}
	
    public ByaccParserFix(Reader r) {
        super(r);
    }

	public static void main(String args[]) throws IOException {
		
		//run("testBlock.gluck");
	    System.out.println("GLUCK SINTAX TESTER");
	    BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
	    ByaccParserFix yyparser;
	    String filename;
	    FileReader file;
		FileWriter fileout;
		
		   
    	System.out.println("\n\n**************");
    	System.out.println(     "File to check: ");

		tree= new Tree("Gluck Tree");

    	try
    	{
    		filename = br.readLine();
    		System.out.println("src/test/java/gluck/"+filename);
    		file = new FileReader("src/test/java/gluck/"+filename);
	    		
    		st= new SymbolTable();
    		yyparser = new ByaccParserFix(file);
	    		
    		fileout = new FileWriter("src/test/java/gluck/"+filename+".st");
    		st.setOutput(fileout);
	    
    		yyparser.yyparse();
    		yyparser.printSymbolTable();    		
			file.close();
			fileout.close();

			tree.printTreeInFile("src/test/java/gluck/"+filename+".dot");
				
    	}catch(IOException e)
    	{
    		System.out.println("Error: File is not valid");
    	}    	
	    	
	    
	}
	
	public static boolean run(String filename) throws IOException 
	{
	
		System.out.println("GLUCK SINTAX TESTER");
	    ByaccParserFix yyparser;
	    FileReader file;
		FileWriter fileout;
		
		   
	   	System.out.println("\n\n**************");
	    System.out.println(     "File to check: ");

		tree= new Tree("Gluck Tree");

	    	System.out.println("src/test/java/gluck/"+filename);
	    	file = new FileReader("src/test/java/gluck/"+filename);
	    		
	    	st= new SymbolTable();
	    	yyparser = new ByaccParserFix(file);
	    		
	    	fileout = new FileWriter("src/test/java/gluck/"+filename+".st");
	    	st.setOutput(fileout);
	    
	    	yyparser.yyparse();
	    	yyparser.printSymbolTable();    		
			file.close();
			fileout.close();

			tree.printTreeInFile("src/test/java/gluck/"+filename+".dot");
			return true;
			
	    
	}
	
}